﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace UMLSynthesizer.EAUMLSynthesizerPlugin.ViewModel
{
    class SelectViewViewModel : WorkspaceViewModel
    {
        #region Fields and RelayCommands

        bool _createNewView;
        IEnumerable<string> _existingModelViewNames;
        IEnumerable<string> _existingModelNames;
        string _selectedModelViewName;
        string _selectedModelName;
        string _selectedViewName;

        RelayCommand _selectViewAndCloseCommand;

        #endregion

        #region Public Interface

        public SelectViewViewModel(IEnumerable<string> existingModelViewNames,
            IEnumerable<string> existingModelNames)
        {
            //check input
            if (existingModelViewNames == null)
                existingModelViewNames = new string[0];
            if (existingModelNames == null ||
                existingModelNames.Count() == 0)
                throw new InvalidOperationException("There must be at least a model");
            foreach (var viewModelName in existingModelViewNames)
            {
                var names = viewModelName.Split(new char[] { '.' });
                if (names.Count() != 2)
                    throw new InvalidOperationException("Every model.view name must has one and only one dot '.'");
                if (names[0].Length == 0 || names[1].Length == 0)
                    throw new InvalidOperationException("Model and view names can't be empty");
            }
            foreach (var modelName in existingModelNames)
            {
                if (modelName.Length == 0)
                    throw new InvalidOperationException("A model name can't be empty");
                if (modelName.Contains('.'))
                    throw new InvalidOperationException("A model name can't have dots '.' inside");
            }

            //Initialize
            CreateNewView = existingModelViewNames.Count() == 0;
            this.ExistingModelViewNames = existingModelViewNames;
            this.ExistingModelNames = existingModelNames;
            this.SelectedModelViewName = (ExistingModelViewNames.Count() != 0 ? ExistingModelViewNames.First() : "");
            this.SelectedModelName = ExistingModelNames.First();
            this.SelectedViewName = "";
        }

        #endregion

        #region Properties

        public bool CreateNewView
        {
            get { return _createNewView; }
            set
            {
                if (value == _createNewView)
                    return;
                _createNewView = value;
                OnPropertyChanged("CreateNewView");
            }
        }

        public IEnumerable<string> ExistingModelViewNames
        {
            get { return _existingModelViewNames; }
            set
            {
                if (value == _existingModelViewNames)
                    return;
                _existingModelViewNames = value;
                OnPropertyChanged("ExistingModelViewNames");
            }
        }
        public IEnumerable<string> ExistingModelNames
        {
            get { return _existingModelNames; }
            set
            {
                if (value == _existingModelNames)
                    return;
                _existingModelNames = value;
                OnPropertyChanged("ExistingModelNames");
            }
        }
        public string SelectedModelViewName
        {
            get { return _selectedModelViewName; }
            set
            {
                if (value == _selectedModelViewName)
                    return;
                _selectedModelViewName = value;
                OnPropertyChanged("SelectedModelViewName");
            }
        }
        public string SelectedModelName
        {
            get { return _selectedModelName; }
            set
            {
                if (value == _selectedModelName)
                    return;
                _selectedModelName = value;
                OnPropertyChanged("SelectedModelName");
            }
        }
        public string SelectedViewName
        {
            get { return _selectedViewName; }
            set
            {
                if (value == _selectedViewName)
                    return;
                _selectedViewName = value;
                OnPropertyChanged("SelectedViewName");
            }
        }

        public bool ExitStateOK { get; private set; }

        #endregion

        #region Commands

        public ICommand SelectViewAndCloseCommand
        {
            get
            {
                if (_selectViewAndCloseCommand == null)
                    _selectViewAndCloseCommand = new RelayCommand(
                        param =>
                        {
                            if (_createNewView)
                                SelectedModelViewName = SelectedModelName + '.' + SelectedViewName;
                            else
                            {
                                var names = SelectedModelViewName.Split(new char[] { '.' });
                                SelectedModelName = names[0];
                                SelectedViewName = names[1];
                            }
                            ExitStateOK = true;
                            CloseCommand.Execute(null);
                        }
                        //,
                        //param =>
                        //{
                        //    return (!CreateNewView && SelectedModelViewName.Length != 0) ||
                        //        (CreateNewView && SelectedModelName.Length != 0 && SelectedViewName.Length != 0 &&
                        //        !ExistingModelViewNames.Contains(SelectedModelName + '.' + SelectedViewName));
                        //}
                        );
                return _selectViewAndCloseCommand;
            }
        }

        #endregion
    }
}
